package com.df.tv.launcher.util;

import java.util.List;

import android.text.TextUtils;

import com.df.tv.launcher.entity.FileInfo;
import com.df.tv.launcher.entity.HomeNavigationData;
import com.df.tv.launcher.entity.MarqueeMsgInfo;
import com.df.tv.launcher.entity.PlayInfo;
import com.df.tv.launcher.exception.TVException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.telecom.tv189.comlib.net.json.entity.BaseEntity;

public class JsonParseUtil {
    private Gson mGson = new Gson();
    private JsonParser mParser = new JsonParser();
    private static JsonParseUtil INSTANCE;

    private JsonParseUtil() {

    }

    public synchronized static JsonParseUtil getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new JsonParseUtil();
        }
        return INSTANCE;
    }

    public static Object parseJson(String json, Class<?> clazz) {
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        return gson.fromJson(json, clazz);
    }

    /**
     * 解析主页内容 Pager数据
     * 
     * @param json
     * @return
     * @throws TVException
     */
    public JsonArray parseHomeContent(String json) {
        return mParser.parse(json).getAsJsonArray();
    }

    public BaseEntity<FileInfo> tmsGetFile(String json) {
        return mGson.fromJson(json, new TypeToken<BaseEntity<FileInfo>>() {}.getType());
    }

    public BaseEntity<JsonArray> getIndex(String json) {
        return mGson.fromJson(json, new TypeToken<BaseEntity<JsonArray>>() {}.getType());
    }

    /**
     * server 得到json
     * 
     * @param json
     * @return
     */
    public BaseEntity<List<HomeNavigationData>> getNavigationData(String json) {
        BaseEntity<List<HomeNavigationData>> data = null;
        if (!TextUtils.isEmpty(json)) {
            data = mGson.fromJson(json, new TypeToken<BaseEntity<List<HomeNavigationData>>>() {}.getType());
        }
        return data;
    }

    public BaseEntity<MarqueeMsgInfo> getMarqueeMsg(String json) {
        return mGson.fromJson(json, new TypeToken<BaseEntity<MarqueeMsgInfo>>() {}.getType());
    }

    public BaseEntity<Object> tmsUpdateLog(String json) {
        return mGson.fromJson(json, new TypeToken<BaseEntity<Object>>() {}.getType());
    }

    public BaseEntity<PlayInfo> getPlayInfo(String json) {
        return mGson.fromJson(json, new TypeToken<BaseEntity<PlayInfo>>() {}.getType());
    }
}
